:- module(fillin, [main/0], [pillow]).

:- use_module(lc_database_impl).

%% Just to make backups! (need pers_data_dir/1)
:- use_module(amos(configuration)).    

:- use_package(hiord).
:- use_module(library(hiordlib)).
:- use_package(functions).
:- function(arith(false)).

:- use_module(simple_html_ui).

main :-
        make_backup,

	Functors = [s_item/7, d_item/3],

	% Apply CGI input
	get_form_input(Info),
	HtmlView = ~(simple_html_ui:apply(Functors, gen_term_desc, Info)),
%
	% Show state
	output_html([
			cgi_reply,
			start,
			title('Amos database editionn'),
			h1('Amos database edition'), 
                        "Enter values in the corresponding fields, then hit the \"Send\" button.  If the form is completely filled in, a new empty row will appear in the next page.  Removing the package name or the dictionary term name will erase the corresponding row. ", 
                        \\,
  			--,
			HtmlView,
			--,
			end
		    ]),
	db_update.

% Metafata

:- multifile fact__names/2.
:- multifile fact__type/1.
:- multifile fact__empty/1.
:- multifile fact__is_empty/1.

:- discontiguous fact__names/2.
:- discontiguous fact__type/1.
:- discontiguous fact__empty/1.
:- discontiguous fact__is_empty/1.

% d_item/3
fact__names(d_item('Term', 'Description', 'Generalizations'), 'Dictionary terms').
fact__type(d_item(atomic, atomic, term_list)).
fact__empty(d_item('', '', [])).
fact__is_empty(d_item('', _, _)).

% s_item/7
fact__names(s_item('Package name','Provides','Requires','URL','Description','Size','License'), 'Packages').
fact__type(s_item(atomic, lowercase_term_list, lowercase_term_list, atomic, atomic, atomic, atomic)).
fact__empty(s_item('', [], [], '', '', '', '')).
fact__is_empty(s_item('', _, _, _, _, _, _)).

% Db interface 

:- multifile db__clean/1.
:- multifile db__fact/1.
:- multifile db__assertz/1.

%db__clean(Fact) :- r_f(Fact), fail ; true. % todo: using r_f_a yields wrong results.. ????
db__clean(Fact) :- db__clean2(Fact).

db__fact(Fact) :- Fact.
db__assertz(Fact) :- a_z_f(Fact).

db__clean2(Fact):-
	r_f(Fact),fail.
db__clean2(_Fact).
